package com.tools.service.impl;

import com.tools.commons.exceptions.BadRequestException;
import com.tools.commons.utils.BeanUtils;
import com.tools.model.Request.UserTaskListRequest;
import com.tools.model.entity.InfoEntity;
import com.tools.model.entity.TasksEntity;
import com.tools.model.entity.UserEntity;
import com.tools.model.entity.UserTasksEntity;
import com.tools.model.params.UserTaskParams;
import com.tools.repository.TaskRepository;
import com.tools.repository.UserRepository;
import com.tools.repository.UserTaskRepository;
import com.tools.security.context.SecurityContextHolder;
import com.tools.service.InfoService;
import com.tools.service.UserTaskSerive;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserTaskServiceImpl implements UserTaskSerive {

    private final UserTaskRepository userTaskRepository;
    private final TaskRepository taskRepository;
    private final InfoService userInfoRepository;
    private final UserRepository userRepository;

    @Override
    public void save(UserTaskParams userTaskParams) {
        UserTasksEntity userTasksEntity = new UserTasksEntity();
        BeanUtils.updateProperties(userTaskParams, userTasksEntity);
        userTasksEntity.setUserId(SecurityContextHolder.getContext().getUserInfo().getUserId());
        userTaskRepository.save(userTasksEntity);
    }

    @Override
    public UserTasksEntity findByUserTaskId(String userTaskId) {
        return userTaskRepository.findByUserTaskId(userTaskId);
    }

    @Override
    public List<UserTasksEntity> findTaskListByUserId(String userid) {
        return userTaskRepository.findByUserId(userid);
    }

    @Override
    public List<UserTasksEntity> findTaskListByTaskId(String taskid) {
        return userTaskRepository.findByTaskId(taskid);
    }

    @Override
    public List<UserTaskListRequest> findUsersByTaskid(String taskId) {
        List<UserTasksEntity> userTasksEntityList = userTaskRepository.findByTaskId(taskId);
        List<UserTaskListRequest> nameLsit = new ArrayList<>();
        for (int i = 0; i < userTasksEntityList.size(); i++) {
            UserTaskListRequest userTaskListRequest = new UserTaskListRequest();
            String userTaskId = userTasksEntityList.get(i).getUserTaskId();
            userTaskListRequest.setUsertaskId(userTaskId);
            String userId =  userTasksEntityList.get(i).getUserId();
            InfoEntity info = userInfoRepository.findByUserId(userId);
            userTaskListRequest.setName(info.getName());
            UserEntity userEntity = userRepository.findByUserId(userId);
            userTaskListRequest.setPhone(userEntity.getPhone());
            userTaskListRequest.setCreateTime(userEntity.getCreateTime());
            userTaskListRequest.setUserId(userEntity.getUserId());
            nameLsit.add(userTaskListRequest);
        }
        return nameLsit;
    }

    @Override
    public UserTasksEntity findByTaskidAndUserid(String taskId, String userId) {
        return userTaskRepository.findByTaskIdAndUserId(taskId, userId);
    }

    @Override
    public UserTasksEntity saveByTaskIdAndPhone(String taskId, String phone) {
        List<UserEntity> byPhone = userRepository.findAll((Specification<UserEntity>) (root, cq, cb) -> cb.and(cb.equal(root.get("isDoctor"), false), cb.equal(root.get("phone"), phone)));
        if (!byPhone.isEmpty()) {
            UserEntity userEntity = byPhone.get(0);
            if (userTaskRepository.findByTaskIdAndUserId(taskId, userEntity.getUserId()) != null) {
                throw new BadRequestException("任务已添加");
            }
            UserTasksEntity userTasksEntity = new UserTasksEntity();
            TasksEntity byTaskId = taskRepository.findByTaskId(taskId);
            // BeanUtils.updateProperties(userTasksEntity, byTaskId);
            userTasksEntity.setTaskId(taskId);
            userTasksEntity.setContent(byTaskId.getContent());
            userTasksEntity.setTitle(byTaskId.getTitle());
            userTasksEntity.setUserId(userEntity.getUserId());
            userTaskRepository.save(userTasksEntity);
        }
        return null;
    }
}
